<template>
  <div style="padding: 20px">
    <!-- 功能区域 -->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary" @click="exportUser">导出</el-button>
    </div>
    <!-- 搜索区域 -->
    <div style="margin: 10px 0">
      <el-input
        v-model="search"
        placeholder="请输入关键字"
        style="width: 20%"
        clearable
      ></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load"
        >查询</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" sortable> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <!-- <el-table-column prop="nickName" label="昵称"> </el-table-column> -->
      <el-table-column prop="sex" label="性别"> </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="address" label="地址" width="200">
      </el-table-column>
      <el-table-column label="角色列表" width="250">
        <template #default="scope">
          <el-select
            v-model="scope.row.roles"
            multiple
            placeholder="请选择"
            style="width: 80%"
          >
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.comment"
              :value="item.id"
            >普通用户</el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="450">
        <template #default="scope">
          <el-button size="mini" type="primary" @click="handleChange(scope.row)"
            >保存角色信息</el-button
          >
          <el-button
            size="mini"
            type="success"
            plain
            @click="showBooks(scope.row.bookList)"
            >更改用户类型</el-button
          >
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <div style="margin: 10px 0">
      <el-dialog v-model="dialogVisible" title="新增信息" width="30%">
        <el-form :v-model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.name" style="width: 80%" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.sex" class="ml-4">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
              <el-radio label="保密">保密</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age" style="width: 80%" />
          </el-form-item>
          <el-form-item label="地址">
            <el-input
              type="textarea"
              v-model="form.address"
              style="width: 80%"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="save"> 提交 </el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "HomeView",
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: "",
      currentPage: 1,
      total: 10,
      tableData: [
        {
          id: "03",
          username: "阿峰",
          address: "福州",
          sex: "男",
          age: "23",
        },
        {
          id: "07",
          username: "潘诗娜",
          address: "广州",
          sex: "女",
          age: "22",
        },
        {
          id: "04",
          username: "刘能法",
          address: "辽宁",
          sex: "男",
          age: "24",
        },
        {
          id: "01",
          username: "兰溪维",
          address: "杭州",
          sex: "男",
          age: "21",
        },
        {
          id: "02",
          username: "阿拉希",
          address: "成都",
          sex: "女",
          age: "24",
        },
      ],
    };
  },
  methods: {
    add() {
      this.dialogVisible = true;
      this.form = {};
    },
    save() {
      request.post("/user", this.form).then((res) => {
        console.log(res);
      });
    },
    handleEdit() {},
    handleSizeChange() {},
    handleCurrentChange() {},
  },
};
</script>

<style>
::v-deep .el-table {
  width: 100%;
}
.el-pagination {
  justify-content: center;
}
</style>
